package com.newland.esp.controller.esp32;

import com.newland.esp.common.util.DataResult;
import com.newland.esp.common.util.SDK.MyObserver;
import com.newland.esp.common.util.SDK.NetWorkBusiness;
import com.newland.esp.common.util.ThreadLocalUtils;
import com.newland.esp.entity.requestEntity.SignInEntity;
import com.newland.esp.entity.responseEntity.User;
import com.newland.esp.entity.responseEntity.base.BaseResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @param
 * @author lw
 * @descripe
 * @date 2021/8/16 21:29
 * @Version 1.0
 **/
@Controller
@CrossOrigin
@RequestMapping("/Cmds")
public class LedController {
    private static NetWorkBusiness netWorkBusiness;

    @RequestMapping("/led/{value}")
    @ResponseBody
    public DataResult controLed(@PathVariable("value") Integer value){

        //dinal 常量的定义
        final String baseUrl = "http://api.nlecloud.com/";
        NetWorkBusiness loginNetWorkBs = new NetWorkBusiness("", baseUrl);
        loginNetWorkBs.signIn(new SignInEntity("17838358410", "xdl3330068"), new MyObserver<User>() {
            @Override
            public void onNext(BaseResponseEntity<User> baseResponseEntity) {
                String token = baseResponseEntity.getResultObj().getAccessToken();
                netWorkBusiness = new NetWorkBusiness(token, baseUrl);
                System.out.println("token: "+ token);
            }
        });
        //发送命令/控制设备 1灯亮 0灯灭
        netWorkBusiness.control("312620", "led_ctl", value, new MyObserver());
        Object status = ThreadLocalUtils.getMap("Status");
        return DataResult.success(status);
    }

}
